const fs = require('fs')

const pagesPath = './src/pages'
const appPath = './src/app.tsx'
const concatName = (p, mode = 1) => {
    let filename = p.substring(p.lastIndexOf('/') + 1)
    return fs.readdirSync(p).map(name => mode === 1 ? `pages/${filename}/${name}/index` : `${name}/index`)
}
const tabbarPath = ['pages/tabbar/index/index', 'pages/tabbar/cart/index', 'pages/tabbar/category/index', 'pages/tabbar/user/index',"pages/tabbar/store/index"]
const dirs = fs.readdirSync(pagesPath).filter(n => n !== 'tabbar')
const subpackages = dirs.map(filename => {
    const root = `${pagesPath}/${filename}`
    let pages = fs.readdirSync(root)
    if (pages.length) {
        return {
            root: `pages/${filename}`,
            pages: concatName(root, 2)
        }
    } else {
        return {}
    }
})
// console.log(tabbarPath)
let apptext = fs.readFileSync(appPath, {
    encoding: 'utf-8'
}).replace(/(config:\s+?Config\s+?=\s+?\{\s+?pages\:\s+?)\[.*\]/gi, `$1${JSON.stringify(tabbarPath)}`)
.replace(/(subPackages:\s+?)\[.*\]/gi, `$1${JSON.stringify(subpackages)}`)

fs.writeFileSync(appPath, apptext)
